package org.db.createdao;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.transmit.StartUtil;

import com.mchange.v2.c3p0.ComboPooledDataSource;


/**
 * 
 * <p>Title: BaseDao   </p>
 * <p>Description: 连接基类</p>
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: prd</p>
 * <p>Create Date/Time: 2014年8月13日 下午8:58:46<p>
 * @author porridge
 * @version 1.0
 * @url www.prdblog.com
 *
 */
public class BaseDao {
	public final static BaseDao dao=new BaseDao();
	private BaseDao(){start();}
	public ComboPooledDataSource ds=new ComboPooledDataSource();
	private final DatabaseUser data=StartUtil.findDatabaseConfig();
	public  final Logger log = Logger.getLogger(BaseDao.class);
	public final String datacls=data.getDatacls();
	
	public Connection getCon() {
		Connection con=null;
		try {
			con=ds.getConnection();
			con.setAutoCommit(false);//加上事务功能
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return con;
		}
	}
	
	public void showsql(String sql){
		if(data.getShowsql().equals("true"))
			log.info(sql);
	}
	private void start(){
		log.info("初始化数据连接......");
		try {
			ds.setDriverClass(data.getLink());
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
		ds.setJdbcUrl("jdbc:"+data.getJdbcFirst()+"://"+data.getUrl()+":"+data.getPort()+"/"+data.getDatabase());
		ds.setUser(data.getUserName());
		ds.setPassword(data.getPassword());
		
		ds.setInitialPoolSize(data.getInitialPoolSize());//初始化连接
		ds.setMaxPoolSize(data.getMaxPoolSize());//允许最大连接数
		ds.setMinPoolSize(data.getMinPoolSize());//允许最小连接数
		ds.setMaxIdleTime(data.getMaxIdleTime());//60秒未使用的连接将被自动丢弃
		ds.setNumHelperThreads(data.getNumHelperThreads());//通过10个多线程开启con
	}
}
